package top.metaspace.singleton.serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author panjun
 * @date 2018/6/21.
 */
public class AvoidSerializationSingletonTest {

	public static void main(String[] args) {
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
			oos.writeObject(AvoidSerializationSingleton.getInstance());
			File file = new File("tempFile");
			ois = new ObjectInputStream(new FileInputStream(file));
			AvoidSerializationSingleton singleton = (AvoidSerializationSingleton) ois.readObject();
			System.out.println(singleton==AvoidSerializationSingleton.getInstance());
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			try {
				if (ois != null) {
					ois.close();
				}
				if (oos != null) {
					oos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

}
